package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.NetworkConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjNetworkAloneTeamChangeMapper;
import com.zb.service.*;
import com.zb.vo.NetworkAloneTeamChangeCheckVo;
import com.zb.vo.NetworkAloneTeamChangeDetailVo;
import com.zb.vo.NetworkAloneTeamChangeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 网点个人团队变更申请表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-02
 */
@Service
public class ZjNetworkAloneTeamChangeServiceImpl extends ServiceImpl<ZjNetworkAloneTeamChangeMapper, ZjNetworkAloneTeamChange> implements ZjNetworkAloneTeamChangeService {
    @Resource
    private ZjNetworkAloneTeamChangeMapper networkAloneTeamChangeMapper;
    @Resource
    private ZjNetworkService networkService;
    @Resource
    private ZjNetworkTeamAuthenFieldValService networkTeamAuthenFieldValService;
    @Resource
    private ZjNetworkEmpRelationService networkEmpRelationService;
    @Resource
    private ZjNetworkAloneAuthenFieldValService networkAloneAuthenFieldValService;

    // 申请成为团队服务商
    @Override
    @Transactional
    public void applyChangeToNetworkTeam(AccounLoginInfo loginInfo, NetworkAloneTeamChangeVo aloneTeamChangeVo) {
        ZjNetwork network = networkService.getById(loginInfo.getNetworkId());
        Assert.notNull(network, "您当前申请成为服务商团队的数据不存在");
        Assert.isTrue(network.getTeamType() == 2, "您当前为服务类型为[ 服务商团队 ]，不可重复申请");
        Assert.isFalse(network.getHasAloneTeamChangeApply(), "您已提交成为服务商团队的申请，正在审核中");

        ZjNetwork updateNetwork = new ZjNetwork();
        updateNetwork.setId(network.getId());
        updateNetwork.setHasAloneTeamChangeApply(true);
        networkService.updateById(updateNetwork);

        ZjNetworkAloneTeamChange aloneTeamChange = new ZjNetworkAloneTeamChange();
        aloneTeamChange.setId(IdWorker.getId());
        aloneTeamChange.setCustAccId(loginInfo.getCustAccId());
        aloneTeamChange.setNetworkId(loginInfo.getNetworkId());
        aloneTeamChange.setApplyType(NetworkConstant.NETWORK_TEAM_TYPE_1);
        aloneTeamChange.setStatus(NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_1);
        aloneTeamChange.setRemark(aloneTeamChange.getRemark());
        aloneTeamChange.setCreateEmp(loginInfo.getName());
        aloneTeamChange.setCancelEmp("");
        aloneTeamChange.setCancelRemark("");
        aloneTeamChange.setReviewEmp("");
        aloneTeamChange.setReviewRemark("");
        this.save(aloneTeamChange);
        networkTeamAuthenFieldValService.save(network.getCustAccId(), network.getId(), aloneTeamChangeVo.getFieldVals());
    }

    // 申请成为个人服务商
    @Override
    public void applyChangeToNetworkAlone(AccounLoginInfo loginInfo, NetworkAloneTeamChangeVo aloneTeamChangeVo) {
        ZjNetwork network = networkService.getById(loginInfo.getNetworkId());
        Assert.notNull(network, "您当前申请成为个人服务商的数据不存在");
        if (network.getTeamType() == 2) {
            Assert.isTrue(false, "您当前为服务类型为[ 个人服务商 ]，不可重复申请");
        }
        Assert.isFalse(network.getHasAloneTeamChangeApply(), "您已提交成为个人服务商的申请，正在审核中");

        QueryWrapper<ZjNetworkEmpRelation> wrapper = new QueryWrapper<>();
        wrapper.select("id")
                .eq("network_id", network.getId());
        Assert.isTrue(networkEmpRelationService.count(wrapper) == 1, "您当前的服务商的团队人员数量超过1人以上，不可申请成功[ 个人服务商 ]");

        ZjNetwork updateNetwork = new ZjNetwork();
        updateNetwork.setId(network.getId());
        updateNetwork.setHasAloneTeamChangeApply(true);
        networkService.updateById(updateNetwork);

        ZjNetworkAloneTeamChange aloneTeamChange = new ZjNetworkAloneTeamChange();
        aloneTeamChange.setId(IdWorker.getId());
        aloneTeamChange.setCustAccId(loginInfo.getCustAccId());
        aloneTeamChange.setNetworkId(loginInfo.getNetworkId());
        aloneTeamChange.setApplyType(NetworkConstant.NETWORK_TEAM_TYPE_2);
        aloneTeamChange.setStatus(NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_1);
        aloneTeamChange.setRemark(aloneTeamChange.getRemark());
        aloneTeamChange.setCreateEmp(loginInfo.getName());
        aloneTeamChange.setCancelEmp("");
        aloneTeamChange.setCancelRemark("");
        aloneTeamChange.setReviewEmp("");
        aloneTeamChange.setReviewRemark("");
        this.save(aloneTeamChange);
        networkAloneAuthenFieldValService.save(network.getCustAccId(), network.getId(), aloneTeamChangeVo.getFieldVals());
    }

    // 根据服务商ID获取服务商个人或团队变更申请
    @Override
    public NetworkAloneTeamChangeDetailVo getNetworkWaitCheckAloneTeamChangeApply(Long networkId) {
        ZjNetworkAloneTeamChange aloneTeamChange = networkAloneTeamChangeMapper.getNetworkWaitCheckAloneTeamChangeApply(networkId);
        Assert.notNull(aloneTeamChange, "当前没有变更申请的数据记录");
        NetworkAloneTeamChangeDetailVo vo = new NetworkAloneTeamChangeDetailVo();
        BeanUtil.copyProperties(aloneTeamChange, vo);
        vo.setCreateTime(aloneTeamChange.getGmtCreate());
        if (aloneTeamChange.getApplyType() == NetworkConstant.NETWORK_TEAM_TYPE_1) {
            vo.setFieldValVos(networkTeamAuthenFieldValService.getByNetworkId(networkId));
        } else {
            vo.setFieldValVos(networkAloneAuthenFieldValService.getByNetworkId(networkId));
        }
        return vo;
    }

    // 审核服务商个人或团队变更申请
    @Override
    @Transactional
    public void checkNetworkWaitCheckAloneTeamChangeApply(AccounLoginInfo loginInfo, NetworkAloneTeamChangeCheckVo changeCheckVo) {
        ZjNetworkAloneTeamChange aloneTeamChange = this.getById(changeCheckVo.getId());
        Assert.notNull(aloneTeamChange, "当前没有变更申请的数据记录");
        Assert.isTrue(aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_1,
                aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_2? "当前变更申请已被审核并通过，禁止重复审核" :
                aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_3? "当前变更申请已取消，禁止审核" : "当前变更申请已被审核并驳回，禁止重复审核"
                );
        ZjNetwork network = new ZjNetwork();
        network.setId(aloneTeamChange.getNetworkId());
        if (changeCheckVo.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_2) {
            network.setTeamType(aloneTeamChange.getApplyType());
        }
        network.setHasAloneTeamChangeApply(false);
        networkService.updateById(network);

        ZjNetworkAloneTeamChange updateAloneTeamChange = new ZjNetworkAloneTeamChange();
        updateAloneTeamChange.setId(aloneTeamChange.getId());
        updateAloneTeamChange.setStatus(changeCheckVo.getStatus());
        updateAloneTeamChange.setReviewRemark(changeCheckVo.getRemark());
        updateAloneTeamChange.setReviewTime(new Date());
        updateAloneTeamChange.setReviewEmp(loginInfo.getName());
        this.updateById(updateAloneTeamChange);
    }

    // 取消变更个人服务商/服务商团队申请
    @Override
    public void cancelChangeToNetworkApply(AccounLoginInfo loginInfo, Long id, RemarkVo remarkVo) {
        ZjNetworkAloneTeamChange aloneTeamChange = this.getById(id);
        Assert.notNull(aloneTeamChange, "当前没有变更申请的数据记录");
        String name = aloneTeamChange.getApplyType() == NetworkConstant.NETWORK_TEAM_TYPE_1 ? "团队服务商" : "个人服务商";
        Assert.isTrue(aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_1,
                aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_2 ? "您变更为[ " + name + " ]的申请已被审核并通过，无法再取消" :
                        aloneTeamChange.getStatus() == NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_3 ? "您变更为[ " + name + " ]的申请已取消，禁止重复取消" : "您变更为[ " + name + " ]的申请已被审核并驳回，无法取消"
        );
        ZjNetwork network = new ZjNetwork();
        network.setId(aloneTeamChange.getNetworkId());
        network.setHasAloneTeamChangeApply(false);
        networkService.updateById(network);

        ZjNetworkAloneTeamChange updateAloneTetamChange = new ZjNetworkAloneTeamChange();
        updateAloneTetamChange.setId(aloneTeamChange.getId());
        updateAloneTetamChange.setStatus(NetworkConstant.NETWORK_ALONE_TEAM_CHANGE_STATUS_3);
        updateAloneTetamChange.setCancelRemark(remarkVo.getRemark());
        updateAloneTetamChange.setCancelTime(new Date());
        updateAloneTetamChange.setCancelEmp(loginInfo.getName());
        this.updateById(updateAloneTetamChange);
    }
}
